package com.ruoyi.supermarket.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supermarket.domain.Qk;
import com.ruoyi.supermarket.mapper.QkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.SupplyMapper;
import com.ruoyi.supermarket.domain.Supply;
import com.ruoyi.supermarket.service.ISupplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供客Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@Service
public class SupplyServiceImpl implements ISupplyService 
{
    @Autowired
    private SupplyMapper supplyMapper;

    @Autowired
    private QkMapper qkMapper;
    /**
     * 查询供客
     * 
     * @param supplyId 供客主键
     * @return 供客
     */
    @Override
    public Supply selectSupplyBySupplyId(Long supplyId)
    {
        return supplyMapper.selectSupplyBySupplyId(supplyId);
    }

    /**
     * 查询供客列表
     * 
     * @param supply 供客
     * @return 供客
     */
    @Override
    public List<Supply> selectSupplyList(Supply supply)
    {
        return supplyMapper.selectSupplyList(supply);
    }

    /**
     * 新增供客
     * 
     * @param supply 供客
     * @return 结果
     */
    @Override
    @Transactional
    public void insertSupply(Supply supply)
    {
        String qz = "";
        String isType = supply.getIsType();
        if(isType.equals("1")){
            qz = "DAG";
        }else if(isType.equals("2")){
            qz = "DAk";
        }
        supply.setIsSh("1");
        supply.setStatus("1");
        supplyMapper.insertSupply(supply);
        String code = qz + String.format("%07d", supply.getSupplyId());
        supply.setSupplyCode(code);
        supplyMapper.updateSupply(supply);

    }

    /**
     * 修改供客
     * 
     * @param supply 供客
     * @return 结果
     */
    @Override
    public int updateSupply(Supply supply)
    {
        supply.setIsSh("1");
        return supplyMapper.updateSupply(supply);
    }

    /**
     * 批量删除供客
     * 
     * @param supplyIds 需要删除的供客主键
     * @return 结果
     */
    @Override
    public int deleteSupplyBySupplyIds(Long[] supplyIds)
    {
        return supplyMapper.deleteSupplyBySupplyIds(supplyIds);
    }

    /**
     * 删除供客信息
     * 
     * @param supplyId 供客主键
     * @return 结果
     */
    @Override
    public int deleteSupplyBySupplyId(Long supplyId)
    {
        return supplyMapper.deleteSupplyBySupplyId(supplyId);
    }

    @Override
    public boolean findSupplyByName(Supply supply) {
        Long supplyId = StringUtils.isNull(supply.getSupplyId())? -1L : supply.getSupplyId();
        Supply info = supplyMapper.findSupplyByName(supply.getSupplyName(),supply.getIsType());
        if (StringUtils.isNotNull(info) && info.getSupplyId().longValue() != supplyId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<Supply> selectGKSupplyList(Long type) {
            return supplyMapper.selectGKSupplyList(type);
    }

    @Override
    @Transactional
    public AjaxResult shSupply(Supply supply) {
        supply.setStatus("0"); //启用
        supplyMapper.updateSupply(supply);
        //插入欠款表
        if(supply.getIsSh().equals("0")){
            Qk qk = new Qk();
            qk.setSupplyId(supply.getSupplyId());
            qk.setQkPrice(BigDecimal.ZERO);
            qkMapper.insertQk(qk);
        }
        return AjaxResult.success();
    }

    @Override
    public List<Supply> selectSupplyListOne(Supply supply) {
        List<String> listSh = new ArrayList<String>();
        listSh.add("1");
        listSh.add("2");
        supply.setIsShList(listSh);
        return supplyMapper.selectSupplyListOne(supply);
    }

    @Override
    public AjaxResult status(Long supplyId) {
        Supply supply = supplyMapper.selectSupplyBySupplyId(supplyId);
        if(supply.getStatus().equals("0")){
            supply.setStatus("1");
        }else{
            supply.setStatus("0");
        }
       supply.setSupplyId(supplyId);
        supplyMapper.updateSupply(supply);
        return AjaxResult.success();
    }

}
